package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceimpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {
    @Autowired
    CategoryBrandService categoryBrandService;
    @Override
    public PageDTO<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key) {
        page=Math.min(page,100);
        rows=Math.max(rows,5);
        Page<Brand> page1 = new Page<>(page, rows);
        //判断是否存在 不存在的模糊查询不执行
        boolean notBlank = StringUtils.isNotBlank(key);
        query().like(notBlank,"name",key).or().eq(notBlank,"letter",key).page(page1);
        List<Brand> records = page1.getRecords();

        return new PageDTO<>(page1.getTotal(),page1.getPages(),BrandDTO.convertEntityList(records));
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
        //添加中间表
        List<CategoryBrand> list = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(list);
    }

    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO) {
        //先删除中间表
        boolean brand_id = categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brandDTO.getId()));
        if (!brand_id){
            throw new LyException(500,"更新品牌失败，删除中间表数据出错");
        }
        //更新数据
        boolean b = updateById(brandDTO.toEntity(Brand.class));
        if (!b){
            throw new LyException(500, "更新品牌失败！");
        }
        //插入中间表
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brandDTO.getId())).collect(Collectors.toList());
        boolean b1 =categoryBrandService.saveBatch(collect);
    }

    @Override
    @Transactional
    public void delectBrandById(Long id) {
        //先删除中间表
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
        //删除品牌表
        removeById(id);
        //返回
    }

    @Override
    public List<BrandDTO> queryBrandByCategory(Long id) {
        List<Brand> list=getBaseMapper().queryByCategoryId(id);
        return BrandDTO.convertEntityList(list);
    }
}
